<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区ID" prop="communityId">
        <el-input
          v-model="queryParams.communityId"
          placeholder="请输入小区ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人员ID" prop="userId">
        

        <el-input
          v-model="queryParams.userId"
          placeholder="请输入人员ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否推荐" prop="isTop">
        <el-input
          v-model="queryParams.isTop"
          placeholder="请输入是否推荐"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务人数" prop="peopleNum">
        <el-input
          v-model="queryParams.peopleNum"
          placeholder="请输入服务人数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="满意度" prop="scoreRate">
        <el-input
          v-model="queryParams.scoreRate"
          placeholder="请输入满意度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:manage_house_agent:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:manage_house_agent:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="manage_house_agentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="小区ID" align="center" prop="communityId" />
      <el-table-column label="人员ID" align="center" prop="userId" />
      <el-table-column label="是否推荐" align="center" prop="isTop" />
      <el-table-column label="状态0停用1启用" align="center" prop="status" />
      <el-table-column label="服务人数" align="center" prop="peopleNum" />
      <el-table-column label="满意度" align="center" prop="scoreRate" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:manage_house_agent:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:manage_house_agent:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改经纪人管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="manage_house_agentRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="代理小区" prop="communityId">
          <el-select
          v-model="form.communityId"
          filterable
          placeholder="选择小区"
          style="width: 100%"
        >
          <el-option
            v-for="item in communityList"
            :key="item.communityId"
            :label="item.name"
            :value="item.communityId"
          />
        </el-select>
        </el-form-item>
        <el-form-item label="人员" prop="userId">
         
          <el-select
          v-model="form.userId"
          filterable
          placeholder="选择人员"
          style="width: 100%"
        >
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          />
        </el-select>
        </el-form-item>
        <el-form-item label="是否推荐" prop="isTop">
          <el-input v-model="form.isTop" placeholder="请输入是否推荐" />
        </el-form-item>
        <el-form-item label="服务人数" prop="peopleNum">
          <el-input v-model="form.peopleNum" placeholder="请输入服务人数" />
        </el-form-item>
        <el-form-item label="满意度" prop="scoreRate">
          <el-input v-model="form.scoreRate" placeholder="请输入满意度" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Manage_house_agent">
import { listManage_house_agent, getManage_house_agent, delManage_house_agent, addManage_house_agent, updateManage_house_agent } from "@/api/manage/manage_house_agent";
import { listUser } from "@/api/system/user";
import { listCommunity } from "@/api/manage/community";
import { authUserSelectAll } from "@/api/system/role";
const { proxy } = getCurrentInstance();

const manage_house_agentList = ref([]);
const userList = ref([]);
const communityList=ref([])
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    communityId: null,
    userId: null,
    isTop: 1,
    status: null,
    peopleNum: null,
    scoreRate: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询经纪人管理列表 */
function getList() {
  loading.value = true;
  listManage_house_agent(queryParams.value).then(response => {
    manage_house_agentList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询用户列表 */
function getUserList() {
  loading.value = true;
  listUser({pageNum:1,pageSize:10000}).then(res => {
    
    userList.value = res.rows;

  });
};

/** 查询小区信息列表 */
function getCommunityList() {
  
  listCommunity(queryParams.value).then(response => {
    communityList.value = response.rows;
   
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    houseAgentId: null,
    communityId: null,
    userId: null,
    isTop: 1,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    peopleNum: null,
    scoreRate: null
  };
  proxy.resetForm("manage_house_agentRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.houseAgentId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加经纪人管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _houseAgentId = row.houseAgentId || ids.value
  getManage_house_agent(_houseAgentId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改经纪人管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["manage_house_agentRef"].validate(valid => {
    if (valid) {
      if (form.value.houseAgentId != null) {
        updateManage_house_agent(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addManage_house_agent(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          authUserSelectAll({ roleId: 102, userIds: form.value.userId }).then(res => {
            
          });
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _houseAgentIds = row.houseAgentId || ids.value;
  proxy.$modal.confirm('是否确认删除经纪人管理编号为"' + _houseAgentIds + '"的数据项？').then(function() {
    return delManage_house_agent(_houseAgentIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/manage_house_agent/export', {
    ...queryParams.value
  }, `manage_house_agent_${new Date().getTime()}.xlsx`)
}
getUserList()
getList();
getCommunityList()
</script>
